//
//  VipInfo4ViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/27.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipInfo4ViewController.h"
#import "VipInfpPersonTableViewCell.h"
#import "SideViewController.h"
#import "SourceAlertView.h"
#import "SelectWorkViewController.h"
#import "AddVenueVipViewController.h"

@interface VipInfo4ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation VipInfo4ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"更多信息";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[VipInfpPersonTableViewCell class] forCellReuseIdentifier:@"VipInfpPersonTableViewCellHeadView"];
    [tableView registerClass:[VipInfpPersonTableViewCell class] forCellReuseIdentifier:@"VipInfpPersonTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

#pragma mark UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSource[section] count];
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VipInfpPersonTableViewCell *cell ;
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"VipInfpPersonTableViewCellHeadView"];
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",self.venueVipModel.Pic,mid_image];
        
        urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:PlaceHold];
        
    }
    else{
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"VipInfpPersonTableViewCell"];                
    }
    
    
    
    
    NSString *imageName = self.dataSource[indexPath.section][indexPath.row];
    if ([imageName isEqualToString:@"教练"]) {
        
        cell.typeImageView.image = [UIImage imageNamed:@"教练1"];
    }
    else{
        
        cell.typeImageView.image = [UIImage imageNamed:self.dataSource[indexPath.section][indexPath.row]];
    }
    
    cell.typeLabel.text = self.dataSource[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *titleName = self.dataSource[indexPath.section][indexPath.row];
    
    if ([titleName isEqualToString:@"会员照片"] || [titleName isEqualToString:@"基本信息"] || [titleName isEqualToString:@"来源"] || [titleName isEqualToString:@"推荐人"]) {
        
        AddVenueVipViewController *addVenueVC = [[AddVenueVipViewController alloc] init];
        
        addVenueVC.venueVipModel = self.venueVipModel;
        
        [self.navigationController pushViewController:addVenueVC animated:YES];
    }
    
    if ([titleName isEqualToString:@"体侧数据"]) {
        
        SideViewController *sideVC = [[SideViewController alloc] init];
        
        sideVC.from = @"vipInfo";
        
        sideVC.venueVipModel = self.venueVipModel;
        
        [self.navigationController pushViewController:sideVC animated:YES];
    }
    
    if ([titleName isEqualToString:@"教练"]) {
        
        [self choseCoaches];
    }
    
//    if ([titleName isEqualToString:@"来源"]) {
//
//        [self sourceBtnClick];
//    }
    
}


#pragma mark 来源
-(void)sourceBtnClick{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    SourceAlertView *sourceAlertView = [[SourceAlertView alloc] init];
    
    sourceAlertView.commitBlock = ^(NSString *str) {
        
        [weakSelf saveMemberkey:@"Source" andValue:str];
    };
    
    [sourceAlertView showWithAnimation];
}

/**
 选择教练
 */
-(void)choseCoaches{
    
    __weak typeof(self)weakSelf = self;
    
    SelectWorkViewController *selectWorkVC = [[SelectWorkViewController alloc] init];
    
    selectWorkVC.from = @"教练";
    
    selectWorkVC.sendSelectWorkerBlock = ^(NSString *workId, NSString *workName) {
        
        [weakSelf saveMemberkey:@"TechUserID" andValue:workName];
    };
    
    [self.navigationController pushViewController:selectWorkVC animated:YES];
}

-(void)saveMemberkey:(NSString *)key andValue:(NSString *)value{
    
    //Post_MemberOperate
    
//    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    VenueModel *venue = [VenueModel venue];
    
//    [params setObject:venue.venueId forKey:@"ShopID"];
//
//    [params setObject:self.venueVipModel.ID forKey:@"ID"];
//
//    [params setObject:value forKey:key];
//
//    [HTTPTool POSTRaw:Post_MemberOperate parameters:params
//
//              success:^(id responseObject){
//
//                  [MBProgressHUD hideHUDForView:self.view];
//
//                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
//
//                      [MBProgressHUD showText:@"添加成功"];
//
//                      [NotificationCenter postNotificationName:@"ReloadVipMember" object:nil];
//
//                      [self.navigationController popViewControllerAnimated:YES];
//                  }
//
//              }
//
//              failure:^(NSError *error){
//
//                  [MBProgressHUD hideHUDForView:self.view];
//              }];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
        [_dataSource addObject:@[@"会员照片",@"体侧数据",@"基本信息"]];
        
        [_dataSource addObject:@[@"教练",@"来源",@"推荐人"]];
    }
    
    return _dataSource;
}

@end
